package initialization

import (
	"blog/app/common"
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"log"
)

// 设置数据库
func SetDB() {
	var err error
	//var loc = "Asia/Shanghai"
	//v,_ := url.ParseQuery(loc)

	connstr := fmt.Sprintf("%s:%s@tcp(%s:%v)/%s?charset=utf8&parseTime=true",
		common.C.DbUser,
		common.C.DbPass,
		common.C.DbHost,
		common.C.DbPort,
		common.C.DbName)
	fmt.Println(connstr)
	DB, err := sql.Open("mysql", connstr)
	if err != nil {
		log.Println("数据库连不上...")
		panic(err)
	}
	DB.SetMaxIdleConns(int(common.C.DbMaxIdleConn))
	DB.SetMaxOpenConns(int(common.C.DbMaxOpenConn))
	err = DB.Ping()
	if err != nil {
		log.Println("连不上网...")
		panic(err)
	}
	log.Printf("connect to database,%s:%v %s", common.C.DbHost, common.C.DbPort, common.C.DbName)
	common.DB = DB
}
